package com.st.mall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.st.mall.product.entity.ProductAttrValueEntity;
import com.st.mall.product.service.ProductAttrValueService;
import com.st.mall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.st.mall.product.service.AttrService;
import com.st.common.utils.PageUtils;
import com.st.common.utils.R;



/**
 * 商品属性
 *
 * @author fg
 * @email f@163.com
 * @date 2023-02-26 20:46:23
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService service;

    /**
     *
     * 获取分类规格参数
     *
     */
    @GetMapping ("/{attrType}/list/{catelogId}")
    public R baseList(@PathVariable("attrType") String type,@PathVariable("catelogId") Long catelogId,@RequestParam Map<String, Object> params){

        PageUtils page = attrService.queryPage(params,catelogId,type);
        return R.ok().put("page", page);
    }

    /**
     *
     * 获取spu规格
     *
     */
    @GetMapping ("/base/listforspu/{spuId}")
    public R getForSpu(@PathVariable("spuId") Long spuI){
        List<ProductAttrValueEntity> forSpu = attrService.getForSpu(spuI);
        return R.ok().put("data",forSpu);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrVo attr = attrService.getDetails(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }
    /**
     * 修改商品规格
     */
    @RequestMapping("/update/{spuId}")
    public R update(@RequestBody List<ProductAttrValueEntity> attrs, @PathVariable("spuId") Long spuId){
        service.updateBySpuId(spuId,attrs);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
